/*
 * test_patch.cpp
 *
 *  Created on: Apr 1, 2014
 *      Author: killerg
 */

#include <event2/util.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>     /* for CHAR_BIT */

#define BITMASK(b) (1 << ((b) % CHAR_BIT))
#define BITSLOT(b) ((b) / CHAR_BIT)
#define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
#define BITCLEAR(a, b) ((a)[BITSLOT(b)] &= ~BITMASK(b))
#define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))
#define BITNSLOTS(nb) ((nb + CHAR_BIT - 1) / CHAR_BIT)

struct data_t {
	char name[8];
	int att[100];
};


bool test_patch() {
	printf("sizeof(data_t):%d\n", sizeof(data_t));

	char bits[BITNSLOTS(sizeof(data_t))];
	memset(bits, 0, sizeof(bits));
	data_t data;
	memset(&data, 0, sizeof(data));
	data.att[4] = -1;
	for (int i=0; i<sizeof(int); ++i)
		BITSET(bits, evutil_offsetof(data_t, att[4]) + i);

	data_t data2;
	memset(&data2, 0, sizeof(data2));

	for (int i=0; i<sizeof(data_t); ++i) {
		if (BITTEST(bits, i)) {
			*((char*)&data2 + i) = *((char*)&data + i);
		}
	}


	printf("offset of name:%d\n", evutil_offsetof(data_t, name[3]));
	printf("offset of att:%d\n", evutil_offsetof(data_t, att[4]));

	return true;
}
